package profit

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
)

type profitRepository struct {
}

func NewProfitRepository() ProfitRepositoryIface {
	return &profitRepository{}
}

// GetTenantProfit 获取租户权益
func (p *profitRepository) GetTenantProfit(ctx context.Context) ([]string, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewProfitServiceClient(conn)
	resp, err := client.GetTenantProfit(ctx, &pb.GetTenantProfitRequest{})
	if err != nil {
		return nil, err
	}

	if resp == nil {
		return nil, errors.New("获取租户权益失败")
	}

	productCodes := make([]string, 0)
	if resp.GetProfits() != nil {
		for _, item := range resp.GetProfits() {
			productCodes = append(productCodes, item.ProductCode)
		}
	}

	return productCodes, nil
}

func (p *profitRepository) GetProfitProvince(ctx context.Context) (*pb.GetProfitProvinceResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewProfitServiceClient(conn)
	resp, err := client.GetProfitProvince(ctx, &pb.GetProfitProvinceRequest{})
	if err != nil {
		return nil, err
	}

	if resp == nil {
		return nil, errors.New("获取数据权益失败")
	}

	return resp, nil
}

func (p *profitRepository) SaveProfitProvince(ctx context.Context, provinceCode []string) error {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewProfitServiceClient(conn)
	_, err = client.SaveProfitProvince(ctx, &pb.SaveProfitProvinceRequest{ProvinceCode: provinceCode})
	if err != nil {
		return err
	}
	return nil
}

func (p *profitRepository) SaveLogoFilePath(ctx context.Context, logoFilePath string) error {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewSiteServiceClient(conn)
	_, err = client.SaveSiteInfo(ctx, &pb.SaveSiteInfoRequest{SiteInfo: &pb.SiteInfo{Logo: logoFilePath}})
	if err != nil {
		return err
	}
	return nil
}

func (p *profitRepository) GetSiteInfo(ctx context.Context) (*pb.SiteInfo, error) {
	siteInfo := pb.SiteInfo{}
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewSiteServiceClient(conn)
	resp, err := client.GetSiteInfo(ctx, &pb.GetSiteInfoRequest{})
	if err != nil {
		return nil, err
	}
	if resp != nil && resp.GetSiteInfo() != nil {
		siteInfo = *resp.GetSiteInfo()
	}
	return &siteInfo, nil
}
